package com.mtons.member.account.action;


import java.util.List;
import java.util.Locale;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mtons.core.entity.po.AccountPO;
import com.mtons.core.interfaces.action.IAccountAction;
import com.mtons.core.interfaces.service.IAccountService;

@Controller
@RequestMapping("/account")
public class AccountAction implements IAccountAction {
	
	private static final Logger logger = Logger.getLogger(AccountAction.class);
	
	@Autowired
	private IAccountService accountService;
	
	@Autowired
	private MessageSource messageSource;

	/* (non-Javadoc)
	 * @see com.mtons.core.interfaces.action.IAccountAction#main()
	 */
	@Override
	public String main() {
		return "index";
	}

	@Override
	public String listAccount(ModelMap model) {
		List<AccountPO> list = this.accountService.findAllAccounts();
		model.addAttribute("accounts", list);
		logger.info("list:"+list);
		return "allaccounts";
	}

	@Override
	public String newAccount(ModelMap model) {
		AccountPO po = new AccountPO();
		model.addAttribute("account", po);
		model.addAttribute("edit", false);
		return "registration";
	}

	@Override
	public String saveAccount(AccountPO po, BindingResult result, ModelMap model) {
		if(result.hasErrors()){
			return "registration";
		}
		if(!this.accountService.isAccountSsnUnique(po.getId(), po.getSsn())){
			FieldError ssnError = new FieldError("account", "ssn", messageSource.getMessage("non.unique.ssn", new String[]{po.getSsn()}, Locale.getDefault()));
			result.addError(ssnError);
			return "registration";
		}
		
		this.accountService.saveAccount(po);
		model.addAttribute("success", "account " + po.getUsername() + "registered successfully");
		return "success";
	}

	@Override
	public String editAccount(String ssn, ModelMap model) {
		AccountPO po = this.accountService.findAccountBySsn(ssn);
		model.addAttribute("account", po);
		model.addAttribute("edit", true);
		return "registration";
	}

	@Override
	public String updateAccount(AccountPO po, BindingResult result, ModelMap model, String ssn) {
		if(result.hasErrors()){
			return "registration";
		}
		if(!this.accountService.isAccountSsnUnique(po.getId(), po.getSsn())){
			FieldError ssnError = new FieldError("account", "ssn", messageSource.getMessage("non.unique.ssn", new String[]{po.getSsn()}, Locale.getDefault()));
			result.addError(ssnError);
			return "registration";
		}
		
		this.accountService.saveAccount(po);
		model.addAttribute("success", "account " + po.getUsername() + "updated successfully");
		return "success";
	}

	@Override
	public String deleteAccount(String ssn) {
		this.accountService.deleteAccount(ssn);
		return "redirect:/list";
	}

}
